package com.monkeybrother.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageCropLeftRight {
    public static void main(String[] args) {
        try {
            // 读取图片文件
            BufferedImage originalImage = ImageIO.read(new File("D:\\hprof\\222.jpg"));
            int width = originalImage.getWidth();
            int height = originalImage.getHeight();
            // 截取左边一半
            BufferedImage leftHalf = originalImage.getSubimage(0, 0, width / 2, height);
            // 截取右边一半
            BufferedImage rightHalf = originalImage.getSubimage(width / 2, 0, width / 2, height);
            // 可以在这里将截取后的图像保存起来
            // 例如保存左边一半的图像
            ImageIO.write(leftHalf, "jpg", new File("left_half.jpg"));
            // 保存右边一半的图像
            ImageIO.write(rightHalf, "jpg", new File("right_half.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}